package com.hwpt.hwaccount.feature.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.hwpt.hwaccount.R;
import com.hwpt.hwaccount.core.data.UserTableObserver;
import com.hwpt.hwaccount.core.data.local.entity.UserEntity;
import com.hwpt.hwaccount.core.utils.AvatarImageUtils;
import com.hwpt.hwaccount.feature.feedback.FeedbackActivity;
import com.hwpt.hwaccount.feature.personalinfo.PersonalInfoActivity;
import com.hwpt.hwaccount.feature.security.SecurityActivity;

public class HomeActivity extends AppCompatActivity implements HomeContract.View {

    private ImageView avatar_image;
    private TextView nicknameText;
    private TextView userId;
    private CardView cardPersonalInfo;
    private CardView cardAccountSecurity;
    private CardView cardFeedback;
    private CardView cardAbout;
    private Button btnLogout;

    private HomeContract.Presenter presenter;
    private UserTableObserver userTableObserver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initViews();
        setupClickListeners();

        HomeContract.Model model = new HomeModel(this);
        presenter = new HomePresenter(model, this); // 传递Context
        presenter.attachView(this);

        presenter.loadUserInfo();
    }

    private void initViews() {
        avatar_image = findViewById(R.id.avatar_image);
        nicknameText = findViewById(R.id.nicknameText);
        userId = findViewById(R.id.userId);
        cardPersonalInfo = findViewById(R.id.cardPersonalInfo);
        cardAccountSecurity = findViewById(R.id.cardAccountSecurity);
        cardFeedback = findViewById(R.id.cardFeedback);
        cardAbout = findViewById(R.id.cardAbout);
        btnLogout = findViewById(R.id.btnLogout);
    }

    private void setupClickListeners() {
        cardPersonalInfo.setOnClickListener(v -> presenter.onPersonalInfoClicked());
        cardAccountSecurity.setOnClickListener(v -> presenter.onAccountSecurityClicked());
        cardFeedback.setOnClickListener(v -> presenter.onFeedbackClicked());
        cardAbout.setOnClickListener(v -> presenter.onAboutClicked());
        btnLogout.setOnClickListener(v -> presenter.onLogoutClicked());
    }

    @Override
    public void showUserInfo(UserEntity user) {
        nicknameText.setText(user.getNickname() != null ? user.getNickname() : "未设置昵称");
        userId.setText(user.getHanWangId());
        AvatarImageUtils.loadAvatarImage(this, user, avatar_image);
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context getContext() {
        return this;
    }


    @Override
    public void navigateToPersonalInfo() {
        Intent intent = new Intent(this, PersonalInfoActivity.class);
        startActivity(intent);
    }

    @Override
    public void navigateToAccountSecurity() {
        Intent intent = new Intent(this, SecurityActivity.class);
        startActivity(intent);
    }

    @Override
    public void navigateToFeedback() {
        Intent intent = new Intent(this, FeedbackActivity.class);
        startActivity(intent);
    }

    @Override
    public void navigateToAbout() {
        // TODO: 跳转到关于页面
        Toast.makeText(this, "关于页面待实现", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void logoutSuccess() {
        finish();
    }
    @Override
    public void showSuccess(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadUserInfo();
    }

    @Override
    protected void onDestroy() {
        if (presenter != null) {
            presenter.onDestroy();
        }
        super.onDestroy();
    }
}